package Domain.keyboardListener;

import java.util.Scanner;
import Domain.stompProtocol.StompClient;
import Domain.stompProtocol.StompFrame;
import Logger.ClientLogger;
import java.util.logging.Level;


public class KeyboardListener implements Runnable
{
	private boolean _shouldClose;
	private StompClient _stompC;
	private Scanner _in; 
	final String _delimiter;

	public KeyboardListener(StompClient stompClient)
	{
		this._delimiter = ";";
		this._stompC = stompClient;
		this._shouldClose = false;
		this._in = new Scanner(System.in);
	}


	public void run()
	{
		while(!getShouldClose())
		{	
			StompFrame toSend;
			String msg = _in.nextLine();
			toSend = processMsg(msg);
			if(toSend != null)
				this._stompC.sendStompFrame(toSend);
				//TODO give the message to the protocol to proccess
		}
	}


	public StompFrame processMsg(String msg)
	{
		StompFrame toSend = null;
		String[] splitMsg =  msg.split(_delimiter);
		
		for (int i = 0; i < splitMsg.length; i++) {
			splitMsg[i]=splitMsg[i].trim();
		}
		
		if(splitMsg[0].equals("connect") && splitMsg.length>2){
			toSend = StompFrame.createConnectFrame(splitMsg[1],splitMsg[2], "");
		}
		else if(splitMsg[0].equals("register") && splitMsg.length >= 3){
			toSend = StompFrame.createRegisterFrame(splitMsg[1],splitMsg[2], "");
		}
		else if(splitMsg[0].equals("disconnect")){ 
			toSend = StompFrame.createDisconnectFrame("");
		}
		else if(splitMsg[0].equals("exit")){ //closes the program at the user 
			toSend = StompFrame.createExitFrame("");
			setShouldClose();
			System.exit(0);
		}
		else if(splitMsg[0].equals("add user") && splitMsg.length >= 1){ 
			toSend = StompFrame.createSendFrame("","fserver","add user ;");
		}
		else if(splitMsg[0].equals("remove user") && splitMsg.length >= 1){ 
			toSend = StompFrame.createSendFrame("","fserver","remove user ;");
		}
		else if(splitMsg[0].equals("add message") && splitMsg.length >= 4){ 
			toSend = StompFrame.createSendFrame("","fserver","add message ; " 
                       + splitMsg[1]+ " ; " + splitMsg[2] + " ; "+ splitMsg[3]);
		}
		else if(splitMsg[0].equals("add thread") && splitMsg.length >= 3 ){ 
			toSend = StompFrame.createSendFrame("","fserver","add thread ; " 
			      + splitMsg[1]+ " ; " + splitMsg[2]);	
		}
		else if(splitMsg[0].equals("add forum") && splitMsg.length >= 2 ){ 
			toSend = StompFrame.createSendFrame("","fserver","add forum ; " + splitMsg[1]);
		}
		else if(splitMsg[0].equals("get messages") && splitMsg.length >= 3 ){ 
			toSend = StompFrame.createSendFrame("","fserver","get messages ; "
					+ splitMsg[1] +" ; "+ splitMsg[2]);
		}
		else if(splitMsg[0].equals("get threads") && splitMsg.length >= 2){ 
			toSend = StompFrame.createSendFrame("","fserver","get threads ; " 
					+ splitMsg[1]);
		}
		else if(splitMsg[0].equals("get forums") && splitMsg.length >= 1 ){ 
			toSend = StompFrame.createSendFrame("","fserver","get forums ; ");
		}
		else if(splitMsg[0].equals("get users") && splitMsg.length >= 1 ){ 
			toSend = StompFrame.createSendFrame("","fserver","get users ; " );
		}
		else if(splitMsg[0].equals("add friend") && splitMsg.length >= 2 ){ 
			toSend = StompFrame.createSendFrame("","fserver","add friend ; "
					+ splitMsg[1]);
		}
		else if(splitMsg[0].equals("remove friend") && splitMsg.length >= 2){ 
			toSend = StompFrame.createSendFrame("","fserver","remove friend ; "
					+ splitMsg[1]);
		}
	    //change type to manager ;[name of user]
		else if(splitMsg[0].equals("manager") && splitMsg.length >= 1) {
			toSend = StompFrame.createSendFrame("","fserver","change type to manager ; " );
		}
	     //change type to default user ;[name of user]*/		
		else if(splitMsg[0].equals("default") && splitMsg.length >= 1) {
			toSend = StompFrame.createSendFrame("","fserver","change type to default ; " );
		}
		else if(msg.startsWith("get type of user") ){
			toSend = StompFrame.createSendFrame("","fserver","get type of user ; " );
                         ClientLogger.write(Level.INFO,"GOT: "+msg);
		}
                else{
                         ClientLogger.write(Level.INFO,"Wrong Command:\n"+ msg +"\nTry Again..");
		}
		return toSend;
		
		
	}

	/**
	 * checks if to close the keyboardListener
	 * @return true if we need to close the keyboardListener
	 */
	public boolean getShouldClose()
	{
		return this._shouldClose;
	}


	/**
	 * sets close client to true
	 */
	public void setShouldClose(){
		this._shouldClose = true;
	}

}
